package com.fzy.auth.service;

import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;

import com.fzy.auth.entity.User;
import com.fzy.auth.mapper.UserListMapper;
import com.fzy.auth.query.UserQuery;
import com.fzy.core.BaseService;
import com.fzy.core.SqlUtils;

public class UserService extends BaseService {

	UserListMapper userListMapper;

	public List list2(UserQuery userQuery) {
		// Session session = sessionFactory.getCurrentSession();
		// Query q = session.getNamedQuery("");
		// q.setParameter(position, val)
		String sql = "select * from fz_user";
		SQLQuery s = getSession().createSQLQuery(sql);
		s.addEntity(com.fzy.auth.entity.User.class);
		return s.list();
	}

	public List list(UserQuery userQuery) {
		String sql = "select * from fz_user where 1=1";
		List params = new LinkedList();
		sql += getConditionSql(userQuery.getName(), "name like", params, true);
		sql += getConditionSql(userQuery.getNickname(), "nick_name like", params, true);
		List l = query(sql, params.toArray(), userListMapper);
		return l;
	}

	public User get(Long id) {
		return (User) getSession().get(User.class, id);
	}

	public int save2() {
		String sql = "insert into fz_user(id, name, password, nick_name, status, created_user, created_date)";
		sql += " values ("+SqlUtils.nextval("s_fz_user_id")+", 'test', 'test', 'test', '1', 0, "+SqlUtils.currentDate()+")";
		return updateSql(sql);
		
//		throw new RuntimeException();
	}

	public void setUserListMapper(UserListMapper userListMapper) {
		this.userListMapper = userListMapper;
	}

}
